/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.purchaseApplyList.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.cti.service.AssetCtiService;
import com.fuyao.cloud.asset.entity.AssetCti;
import com.fuyao.cloud.asset.entity.AssetPurchaseapply;
import com.fuyao.cloud.asset.entity.AssetPurchaseapplyList;
import com.fuyao.cloud.asset.vo.FyAssetPurchaseapplyListExcelVO;
import com.fuyao.cloud.asset.purchaseApplyList.mapper.AssetPurchaseapplyListMapper;
import com.fuyao.cloud.asset.purchaseApplyList.service.AssetPurchaseapplyListService;
import com.fuyao.cloud.asset.vo.apply.PurchaseapplyListPageVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 采购申请清单
 *
 * @author 时间的朋友
 * @date 2021-11-22 13:58:57
 */
@Service
public class AssetPurchaseapplyListServiceImpl extends ServiceImpl<AssetPurchaseapplyListMapper, AssetPurchaseapplyList> implements AssetPurchaseapplyListService {

	@Resource
	private AssetCtiService fyAssetCtiService;

	@Override
	public List<FyAssetPurchaseapplyListExcelVO> listExcel(AssetPurchaseapplyList fyAssetPurchaseapplyList) {
		return this.baseMapper.listExcel(fyAssetPurchaseapplyList);
	}

	@Override
	public void saveOrUpdateApplyList(AssetPurchaseapply entity) {
		List<AssetCti> ctiList = fyAssetCtiService.list();
		List<AssetPurchaseapplyList> purchaseapplyListList = entity.getPurchaseapplyList();
		if (CollUtil.isNotEmpty(purchaseapplyListList)) {
			purchaseapplyListList.forEach(p -> {
				p.setStatus(entity.getStatus());
				p.setBatchnumber(entity.getBatchnumber());
				p.setParentId(entity.getId());
				p.setParentCaseid(entity.getCaseid());
				Optional<AssetCti> ctiOptional = ctiList.stream().filter(cti -> cti.getId().equals(p.getCtiId())).findFirst();
				if (ctiOptional.isPresent()) {
					AssetCti cti = ctiOptional.get();
					p.setCtiName(cti.getName());
					p.setClassifiedCode(cti.getFullcode());
					p.setClassified(cti.getFullname());
				}
			});
			super.saveOrUpdateBatch(purchaseapplyListList);
		}
	}

	@Override
	public List<PurchaseapplyListPageVO> listApplyPageVO(AssetPurchaseapply entity) {
		List<AssetPurchaseapplyList> purchaseapplyLists = this.list(Wrappers.<AssetPurchaseapplyList>query().lambda().eq(AssetPurchaseapplyList::getParentId,entity.getId()));
		if (CollUtil.isNotEmpty(purchaseapplyLists)) {
			return purchaseapplyLists.stream().map(r -> {
				PurchaseapplyListPageVO pageVO = new PurchaseapplyListPageVO();
				BeanUtil.copyProperties(r, pageVO);
				pageVO.setType(entity.getType());
				pageVO.setSupplierId(entity.getSupplierId());
				pageVO.setSupplierName(entity.getSupplierName());
				pageVO.setMethod(entity.getMethod());
				pageVO.setCurrencyType(entity.getCurrencyType());
				pageVO.setBudgetId(entity.getBudgetId());
				pageVO.setBudgetNumber(entity.getBudgetNumber());
				pageVO.setReceivinfo(entity.getReceivinfo());
				return pageVO;
			}).collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
}
